package http

import "encoding/json"

type Result struct {
	Data interface{} `json:"data"`
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
}

func ErrorMessage(msg string) *Result {
	return &Result{
		Code: 400,
		Msg:  msg,
	}
}

func ErrorCode(code int, msg string) *Result {
	return &Result{
		Code: code,
		Msg:  msg,
	}
}

func ErrorDataCode(code int, data interface{}) *Result {
	return NewError(code, "Failure", data)
}

func ErrorDataMessage(msg string, data interface{}) *Result {
	return NewError(400, msg, data)
}

func ErrorData(data interface{}) *Result {
	return ErrorDataCode(400, data)
}

func NewError(code int, msg string, data interface{}) *Result {
	return &Result{
		Code: code,
		Msg:  msg,
		Data: data,
	}
}

func NewSuccess(code int, msg string, data interface{}) *Result {
	return &Result{
		Code: code,
		Msg:  msg,
		Data: data,
	}
}

func SuccessCode(code int) *Result {
	return &Result{
		Code: code,
		Msg:  "Success",
	}
}

func SuccessMsg(msg string) *Result {
	return &Result{
		Code: 200,
		Msg:  msg,
	}
}

func SuccessData(data interface{}) *Result {
	return &Result{
		Code: 200,
		Msg:  "Success",
		Data: data,
	}
}

func (r *Result) ToJsonBytes() []byte {
	marshal, err := json.Marshal(r)
	if err != nil {
		return []byte(err.Error())
	}
	return marshal
}
